import { cardCache,getBusinessInfo,toast} from 'nc-lightapp-front';
import {CARD} from "../../constant";
import { DOCLIST, STATUS} from "../../../../common/constant";
import { byPkdeptFiter, byPkorgFiter, fiterDefdoc, fiterFrkm, fiterLrzxOrg } from '../../../../common/util/fiterUtils';
let { getDefData } = cardCache;
/**
* @description: 表头编辑前事件
* @param: moduleId 区域编码
* @param: key 当前字段编码
* @return: 布尔 true表示可编辑
*/
export function beforeHeadEvent(props, moduleId, key, value, data) {

        let pk_org = null;
        let pk_accountingbook = null;
        let pk_dept = null;
        let where = null;
        let fiterType = null;
        let isMultiSelectedEnabled = false;

        switch (key) {
                case 'ass'://贷方科目
                        pk_accountingbook = getDefData('accbook', 'accbook');
                        pk_org = props.form.getFormItemsValue(CARD.form_id, 'pk_org').value;
                        fiterType = DOCLIST.qtcbdf;
                        fiterFrkm(props, CARD.card_dfkm, key, pk_org, pk_accountingbook, fiterType);
                        break;

                case 'def5'://报销人
                        //根据组织，录单利润中心过滤
                        pk_org = props.form.getFormItemsValue(moduleId, 'pk_org').value;
                        pk_dept = props.form.getFormItemsValue(moduleId, 'dept').value;
                        if(pk_dept == ''){
                                toast({ color: STATUS.warning, content: '请先维护录单利润中心！' });
                                return false;
                        }

                        byPkdeptFiter(props, moduleId, key, pk_org, pk_dept, false);
                        break;

                case 'dept'://录单利润中心
                        //根据组织过滤
                        pk_org = props.form.getFormItemsValue(moduleId, 'pk_org').value;
                        byPkorgFiter(props, moduleId, key, pk_org, isMultiSelectedEnabled);
                        break;
                case 'def12'://现金人员档案
                        //根据组织过滤
                        pk_org = props.form.getFormItemsValue(moduleId, 'pk_org').value;
                        byPkorgFiter(props, moduleId, key, pk_org, isMultiSelectedEnabled);
                        break;

                case 'def6'://承担法人公司
                        //业务单据只能显示法人组织，模板直接改成参照财务组织
                        break;

                case 'def7'://费用承担利润中心
                        fiterLrzxOrg(props, moduleId, key, false, isMultiSelectedEnabled);
                        break;

                case 'def4'://报销部门
                        //利润中心部门自定义档案参照过滤
                        pk_dept = props.form.getFormItemsValue(CARD.form_id, 'dept').value;
                        where = "def1 in (select pk_org from org_orgs where name in (select name from org_orgs where pk_org='"+pk_dept+"')) and def7 = 'Y'";
                        if(pk_dept == ''){
                                toast({ color: STATUS.warning, content: '请先维护录单利润中心！' });
                                return false;
                        }
                        
                        fiterDefdoc(props, moduleId, key, where);
                        break;
                        
                case 'accbank'://银行账号
                        pk_org = props.form.getFormItemsValue(moduleId, 'pk_org').value;
                        byPkorgFiter(props, moduleId, key, pk_org, false);
                        break;

                default:
                        break;
        }

        return true;
}